﻿<%@ Page Title="" Language="C#" MasterPageFile="~/mpTaskPanel.Master" AutoEventWireup="true"
    CodeBehind="ReportDialog.aspx.cs" Inherits="WebUI.ProjectManager.ReportDialog"
    Theme="Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <link href="../css/smoothness/jquery-ui-1.8.7.custom.css" rel="stylesheet" type="text/css" />
    <link href="ProjectManager.css" rel="stylesheet" type="text/css" />
    <link href="ReportDialog.css" rel="stylesheet" type="text/css" />
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="HeaderPlace" runat="server">
    <asp:Literal ID="TitlePlace" runat="server"></asp:Literal>
    <div id="indicator" class="play">
    </div>
    <div id="refreshButton">
    </div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlace" runat="server">
    <asp:Panel ID="ContentPanel" runat="server" CssClass="mainLayout">
        <asp:ScriptManagerProxy ID="scriptManagerProxy" runat="server">
            <Scripts>
                <asp:ScriptReference Path="~/Scripts/jquery-1.5.1.min.js" />
                <asp:ScriptReference Path="~/Scripts/jquery-ui-1.8.7.custom.min.js" />
                <asp:ScriptReference Path="~/Scripts/jquery.ui.datepicker-he.js" />
                <asp:ScriptReference Path="~/Scripts/jquery.maskedinput-1.3.min.js" />
                <asp:ScriptReference Path="~/Scripts/tools.js" />
                <asp:ScriptReference Path="~/Scripts/jqTableSorter.js" />
                <asp:ScriptReference Path="~/Scripts/uitablefilter.js" />
            </Scripts>
        </asp:ScriptManagerProxy>

        <script type="text/javascript">
            var waitsNum = 0;

            var layoutId = "<%= LayoutId %>";
            var sessionId = "<%= SessionId %>";
            var mapName = "<%= MapName %>";

            var projects;
            var uidList;

            var currentProject;

            var STATUS_COLOR = [
                                   "#FFFFFF",
                                   "#FFFBBD",
                                   "#DEDFDE",
                                   "#9CDBEF",
                                   "#FFA2B5",
                                   "#AAFFAA"
                               ];

            $("document").ready(function() {
                $("input[type=button]").button();
                $(".datepicker").width($(".datepicker").width() - 20);
                CreateDatePickers(true);

                $("#projectLayers").change(function() { GetProjects($(this).val()); });
                $("#projectGroup").change(function() { GetProjectLayers($(this).val()) }).trigger("change");

                GetStatuses();
                //GetProjects("0");
                $("#btnFilter").click(function() {
                    var filterName = $("#projectName").val();
                    var filterStatus = $("#StatusId").val();
                    var filterDateFrom = $("#DtFrom").datepicker("getDate");
                    var filterDateTo = $("#DtTo").datepicker("getDate");
                    BuildResults(filterName, filterStatus, filterDateFrom, filterDateTo);
                });
                $("#btnReset").click(function() {
                    $("#projectName").val("");
                    $("#StatusId").val("open");
                    $("#DtFrom").val("");
                    $("#DtTo").val("");
                    $(":radio:checked").removeAttr("checked");
                    $("#rDtStart").attr("checked", "checked");
                    BuildResults(false, "open", false, false);
                    SetFilter('');
                });

                $("#btnShowOnMap").click(function() { SetFilter(uidList); });
                $("#btnReport").click(ShowReport);
                $("#refreshButton").click(AttachLayer);
				var width = $(".withButton").width() - 30;
                $(".withButton").css("min-width", width).css("max-width", width).width(width);
                $("#editLayer").click(ShowMarkupEditor);
            });
            function AddToWaits() {
                waitsNum++;
                if (!$("#indicator").hasClass("play"))
                    $("#indicator").addClass("play");
                if (!$("#refreshButton").hasClass("hidden"))
                    $("#refreshButton").addClass("hidden");
            }
            function RemoveFromWaits() {
                waitsNum--;
                if (waitsNum <= 0) {
                    $("#indicator").removeClass("play");
                    $("#refreshButton").removeClass("hidden");
                    waitsNum = 0;
                }
            }
            function GetProjectLayers(group) {
                AddToWaits();
                var data = "layoutId=" + encodeURIComponent(layoutId)
                         + "&groupName=" + encodeURIComponent(group);
                $.ajax({
                    async: false,
                    url: 'ProjectManagerService.asmx/GetProjectLayers',
                    type: "POST",
                    data: data,
                    success: OnProjectLayersLoaded,
                    error: OnError
                });
            }
            function OnProjectLayersLoaded(data) {
                var features = $.parseJSON(data.text);
                $("#projectLayers").empty();
                $("<option>").attr("value", "0").text("כל השכבות").appendTo("#projectLayers");
                $(features).each(function(i, j) {
                    var opt = $("<option>").attr("value", j.Key).text(j.Value);
                    if (i == 0) opt.attr("selected", "selected");
                    opt.appendTo("#projectLayers");
                });

                $("#projectLayers").trigger("change"); //GetProjects
                RemoveFromWaits();
            }
            function GetProjects(projectId) {
                AddToWaits();
                var data = "layoutId=" + encodeURIComponent(layoutId)
                         + "&groupName=" + encodeURIComponent($("#projectGroup").val())
                         + "&projectId=" + encodeURIComponent(projectId);
                $.ajax({
                    url: 'ProjectManagerService.asmx/GetProjects',
                    type: "POST",
                    data: data,
                    success: OnProjectsLoaded,
                    error: OnError
                });
                AttachLayer();
                SetFilter('');
            }
            function ShowReport() {
                var data = "LAYOUT=" + encodeURIComponent(layoutId)
                         + "&SESSION=" + encodeURIComponent(sessionId)
                         + "&MAPNAME=" + encodeURIComponent(mapName)
                         + "&GROUP=" + encodeURIComponent($("#projectGroup").val())
                         + "&PROJECTID=" + encodeURIComponent($("#projectLayers").val())
                         + "&UIDLIST=" + encodeURIComponent(uidList);
                window.open("./ProjectReport.aspx?" + data,
                                "ProjectsReport",
                                "toolbar=0,fullscreen=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1"
                           );
            }
            function OnProjectsLoaded(data) {
                projects = $.parseJSON(data.text);
                $("#projectList").tablesorter();
                BuildResults(false, "open", false, false);
            }
            function BuildResults(filterName, filterStatus, filterDateFrom, filterDateTo) {
                $("#projectList tbody").empty();
                if (projects != null) {
                    uidList = "";
                    $(projects).each(function(i, j) {
                        if (CheckFilter(j, filterName, filterStatus, filterDateFrom, filterDateTo)) {
                            var rowColor;
                            switch (j.StatusGroup) {
                                case "יזום":
                                    rowColor = STATUS_COLOR[5];
                                    break;
                                case "תכנון מוקדם":
                                    rowColor = STATUS_COLOR[0];
                                    break;
                                case "תכנון סופי":
                                    rowColor = STATUS_COLOR[1];
                                    break;
                                case "תכנון מפורט":
                                    rowColor = STATUS_COLOR[2];
                                    break;
                                case "פיקוח עליון על ביצוע":
                                    rowColor = STATUS_COLOR[3];
                                    break;
                            }
                            if (JsonDateFormat(j.DtDue) < new Date())
                                rowColor = STATUS_COLOR[4];
                            var row = $("<tr>").click(function() {
                                currentProject = j;
                                $(".activeRow").removeClass("activeRow");
                                $(this).addClass("activeRow");
                                SelectProject();
                            }).appendTo("#projectList tbody");
                            $("<td>").text(j.Name).css("background-color", rowColor).appendTo(row);
                            $("<td>").text(j.Status).css("background-color", rowColor).appendTo(row);
                            uidList += uidList.length > 0 ? "," : "";
                            uidList += j.UID;
                        }
                    });
                }
                $("#projectList").trigger("update");
                RemoveFromWaits();
            }
            function AttachLayer() {
                AddToWaits();
                var data = "session=" + encodeURIComponent(sessionId)
                             + "&map=" + encodeURIComponent(mapName)
                             + "&layout=" + encodeURIComponent(layoutId)
                             + "&projectId=" + encodeURIComponent($("#projectLayers").val());
                $.ajax({
                    url: 'ProjectManagerService.asmx/AttachLayer',
                    type: "POST",
                    data: data,
                    success: function() { parent.parent.mapFrame.Refresh(); RemoveFromWaits(); },
                    error: OnError
                });
            }
            function SelectProject() {
                AddToWaits();
                var data = "session=" + encodeURIComponent(sessionId)
                             + "&map=" + encodeURIComponent(mapName)
                             + "&layout=" + encodeURIComponent(layoutId)
                             + "&projectId=" + currentProject.Id;
                $.ajax({
                    url: 'ProjectManagerService.asmx/SelectProject',
                    type: "POST",
                    data: data,
                    success: SetSelectionXml,
                    error: OnError
                });
            }
            function SetSelectionXml(data) {
                parent.parent.mapFrame.SetSelectionXML(data.text);
                parent.parent.mapFrame.ZoomSelection();
                RemoveFromWaits();
            }

            function CheckFilter(j, filterName, filterStatus, filterDateFrom, filterDateTo) {
                var DtDue = null;
                var DtEnd = null;
                if (j.DtDue) {
                    DtDue = JsonDateFormat(j.DtDue);
                }
                if (j.DtEnd) {
                    DtEnd = JsonDateFormat(j.DtEnd);
                }

                if (filterName && j.Name.indexOf(filterName) == -1) return false;
                if (filterStatus == "open") {
                    if (DtEnd != null) return false;
                }
                else if (filterStatus == "close") {
                    if (DtEnd == null) return false;
                }
                else if (filterStatus == "lag") {
                    if (DtDue > new Date()) return false;
                }
                else if (filterStatus > 0 && j.StatusId != filterStatus) return false;

                dateFilterType = $("input:radio:checked").attr("id");
                var date = JsonDateFormat(j[dateFilterType.substr(1)]);

                if (filterDateFrom) {
                    if (date < filterDateFrom) return false;
                }
                if (filterDateTo) {
                    if (date > filterDateTo) return false;
                }
                return true;
            }

            function SetFilter(uidlist) {
                AddToWaits();
                var data = "session=" + encodeURIComponent(sessionId)
                             + "&map=" + encodeURIComponent(mapName)
                             + "&layout=" + encodeURIComponent(layoutId)
                             + "&layerId=" + encodeURIComponent($("#projectLayers").val())
                             + "&uidList=" + encodeURIComponent(uidlist);
                $.ajax({
                    url: 'ProjectManagerService.asmx/SetFilter',
                    type: "POST",
                    data: data,
                    success: function() {
                        parent.parent.mapFrame.Refresh();
                        RemoveFromWaits();
                    },
                    error: OnError
                });
            }
            function ShowMarkupEditor() {
                var data = "session=" + encodeURIComponent(sessionId)
                             + "&map=" + encodeURIComponent(mapName)
                             + "&layout=" + encodeURIComponent(layoutId)
                             + "&layerId=" + encodeURIComponent($("#projectLayers").val())
                $.ajax({
                    url: 'ProjectManagerService.asmx/GetLayerDefinition',
                    type: "POST",
                    data: data,
                    success: function(response) {
                        var layerDefinition = response.text;
                        var link = "../../mapguide/mapviewernet/Extensions/markup/editmarkup.aspx"
                                 + "?SESSION=" + encodeURIComponent(sessionId)
                                 + "&MAPNAME=" + encodeURIComponent(mapName)
                                 + "&LAYOUT=" + encodeURIComponent(layoutId)
                                 + "&OPENMARKUP=" + encodeURIComponent(layerDefinition)
                                 + "&RETURNURL=" + encodeURIComponent(location);
                        location = link;
                    },
                    error: OnError
                });
            }
            function OnError(data) {
                $("#indicator").removeClass("play");
            }
        </script>

        <div id="layout">
            <fieldset>
                <div class="groupHeader">
                    בחר שכבה</div>
                <div class="field">
                    <dl>
                        <dt>
                            <label for="projectGroup">
                                קבוצה</label></dt>
                        <dd>
                            <select id="projectGroup" class="fullWidth">
                                <option value="0">כל הפרוייקטים</option>
                                <asp:Literal ID="litProjectGroupsOptions" runat="server" />
                            </select>
                        </dd>
                        <dt>
                            <label for="projectLayers">
                                שכבה</label></dt>
                        <dd>
                            <select id="projectLayers" class="fullWidth withButton">
                                <option value="0">כל השכבות</option>
                            </select>
                            <img id="editLayer" alt="עדכן שכבה" src="../Images/markup_submit.gif" style="cursor:pointer"/>
                        </dd>
                    </dl>
                </div>
            </fieldset>
            <fieldset>
                <div class="groupHeader">
                    חיפוש לפי:</div>
                <dl>
                    <dt>
                        <label for="projectName">
                            שם</label></dt>
                    <dd>
                        <input id="projectName" class="fullWidth" />
                    </dd>
                    <dt>
                        <label for="StatusId">
                            סטטוס</label></dt>
                    <dd class="odd">
                        <select id="StatusId">
                            <option value="open">פרוייקטים פתוחים</option>
                            <option value="close">פרוייקטים סגורים</option>
                            <option value="lag">פרוייקטים בפיגור</option>
                            <option value="0">הכל</option>
                            <asp:Literal ID="statusPlace" runat="server" />
                        </select>
                    </dd>
                </dl>
                <div style="clear: both">
                </div>
                <div style="text-align: right">
                    <table>
                        <tr>
                            <td>
                                <b>תאריך</b>
                                <input type="radio" name="DateGroup" id="rDtStart" checked="checked" />
                                <label for="rDtStart">
                                    התחלה</label><br />
                            </td>
                            <td>
                                <input type="radio" name="DateGroup" id="rDtDue" />
                                <label for="rDtDue">
                                    ס.צפוי</label><br />
                            </td>
                            <td>
                                <input type="radio" name="DateGroup" id="rDtEnd" />
                                <label for="rDtEnd">
                                    סיום</label><br />
                            </td>
                        </tr>
                    </table>
                    <table class="dategroup" align="left">
                        <tr>
                            <td style="width: auto">
                                מ:
                            </td>
                            <td>
                                <input type="text" class="datepicker" id="DtFrom" />
                            </td>
                            <td>
                                עד:
                            </td>
                            <td>
                                <input type="text" class="datepicker" id="DtTo" />
                            </td>
                        </tr>
                    </table>
                </div>
                <div style="clear: both">
                </div>
                <div class="bottomButtonsPanel" style="text-align: center">
                    <input type="button" id="btnFilter" value="בצע" />
                    <input type="button" id="btnReset" value="אפס" />
                </div>
            </fieldset>
            <fieldset>
                <div class="groupHeader">
                    רשימת תוצאה</div>
                <div class="field" style="max-height: 250px; overflow: auto">
                    <table id="projectList" class="tablesorter">
                        <thead>
                            <tr>
                                <th>
                                    שם פרוייקט
                                </th>
                                <th>
                                    סטטוס
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </fieldset>
            <div class="formFooter">
                <input type="button" id="btnShowOnMap" value="הצג במפה" />
                <input type="button" id="btnReport" value="הצג דו''ח" />
            </div>
            <div>
                <div class="label" style="text-align: right;">
                    מקרא:</div>
                <div class="legendEntry">
                    <div class="colorSquare" style="background-color: #AAFFAA;">
                    </div>
                    יזום
                </div>
                <div class="legendEntry">
                    <div class="colorSquare" style="background-color: #FFFFFF;">
                    </div>
                    תכנון מוקדם
                </div>
                <div class="legendEntry">
                    <div class="colorSquare" style="background-color: #FFFBBD;">
                    </div>
                    תכנון סופי
                </div>
                <div class="legendEntry">
                    <div class="colorSquare" style="background-color: #DEDFDE;">
                    </div>
                    תכנון מפורט
                </div>
                <div class="legendEntry">
                    <div class="colorSquare" style="background-color: #9CDBEF;">
                    </div>
                    פיקוח עליון על ביצוע
                </div>
                <div class="legendEntry">
                    <div class="colorSquare" style="background-color: #FFA2B5;">
                    </div>
                    בפיגור
                </div>
            </div>
        </div>
    </asp:Panel>
</asp:Content>
